locals {
  app_names = [var.tenant_name, format("%s_巡检", var.tenant_name)]

  reqs = [for o in local.app_names :
    {
      request_body = templatefile("${path.module}/body.json.tpl", {
        appName         = o
        SignName        = var.tenant_name
        accessKeyId     = var.accessKeyId
        accessKeySecret = var.accessKeySecret
      })
    }
  ]
}

data "httpclient_request" "this" {
  count = length(local.reqs)

  url = var.url
  request_headers = {
    Content-Type : "application/json",
  }
  request_method = "POST"
  request_body   = lookup(local.reqs[count.index], "request_body")

}

locals {
  appId     = lookup(jsondecode(element(data.httpclient_request.this.*.response_body, 0)), "appId")
  xj_app_id = lookup(jsondecode(element(data.httpclient_request.this.*.response_body, 1)), "appId")
}